package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired SeckillService seckillService;

    /**
     * 秒杀抢购
     */
    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,    //秒杀商品id
                     @RequestParam("key") String key,    //随机码
                     @RequestParam("num") Integer num,    //要秒杀抢购商品的数量
                     Model model){
        //判断是否登录(登录拦截器已经自动处理)
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }


    /**
     * 查询当前商品是否参与秒杀活动
     */
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSecKillInfo(@PathVariable("skuId") Long skuId){
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        SeckillSkuRedisTo to = seckillService.getSkuSecKillInfo(skuId);
        return R.ok().setData(to);
    }


    /**
     * 查询当前时间可以参与的秒杀商品信息
     */
    @ResponseBody
    @GetMapping(value = "/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> vos = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(vos);
    }}
